// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import {defaultType} from "./defaultTypeMediator";

const {ccclass, property} = cc._decorator;

export enum triggerType {
    normal = 0,
    delay = 1,
    show = 2,
    stoneTrigger = 3,
    boxTrigger = 4,
    crashKey = 5,
    crashJerrican = 6,
    crashGhost = 7,

}

@ccclass
export default class triggerMediator extends cc.Component {

    @property({
        type : cc.Enum(triggerType),
        displayName : "触发器的类型"
    })
    curTriggerType : triggerType = triggerType.normal;

    @property({displayName : "仅检测人物"})
    checkRole : boolean = false;

    @property({type : cc.Component.EventHandler})
    toolEvent : cc.Component.EventHandler[] = [];

    @property({displayName : "左右检测判断"})
    dirJudge : boolean = false;

    @property({displayName : "钥匙只和钥匙只和人发生碰撞"})
    crashRole = false;

    @property({displayName : "删除自己"})
    deleteSelf = false;

    @property({displayName : "幽灵是否发生碰撞"})
    checkGhost : boolean = false;

    @property({displayName : "仅和幽灵发生碰撞"})
    onlyCheckGhost : boolean = false;

    @property({displayName : "人物和幽灵都检测"})
    allCheck : boolean = false;

    standing : boolean = false;

    onBeginContact(contact, self, other) {
        if(this.allCheck && (other.node.group == "ghost" || other.node.group == "role")){
            cc.Component.EventHandler.emitEvents(this.toolEvent, [self.node,other.node]);
            if(this.deleteSelf){
                this.node.active = false;
            }
            return;
        }

        if(this.checkRole && other.node.group == "ghost") return;
        if(this.onlyCheckGhost && other.node.group == "role") return;

        if(this.checkRole && other.node.group == "role"){
            cc.Component.EventHandler.emitEvents(this.toolEvent, [self.node,other.node]);
            if(this.deleteSelf){
                this.node.active = false;
            }
            return;
        }


        if(this.onlyCheckGhost && other.node.group == "ghost"){
            cc.Component.EventHandler.emitEvents(this.toolEvent, [self.node,other.node]);
            if(this.deleteSelf){
                this.node.active = false;
            }
            return;
        }

        if(other.node.group == "ghost" && this.curTriggerType == triggerType.crashGhost){
            if(this.deleteSelf){
                this.node.active = false;
            }
            cc.game.emit("shieldPressKey", true);
            cc.game.emit("becomeGhost",false);
            cc.Component.EventHandler.emitEvents(this.toolEvent, [self.node,other.node]);
        }

        if(other.node.group == "ghost" && this.curTriggerType == triggerType.normal){
            cc.Component.EventHandler.emitEvents(this.toolEvent, [self.node,other.node]);
            if(this.deleteSelf){
                this.node.active = false;
            }
            return;
        }

        if(other.node.group == "key" && this.curTriggerType == triggerType.crashKey){
            cc.Component.EventHandler.emitEvents(this.toolEvent, [self.node,other.node]);
            return;
        }

        if(other.node.group == "jerrican" && this.curTriggerType == triggerType.crashJerrican){
            cc.Component.EventHandler.emitEvents(this.toolEvent, [self.node,other.node]);
            return;
        }

        if(other.node.group == "default"){
            if(this.curTriggerType == triggerType.stoneTrigger && other.node.getComponent("defaultTypeMediator").curType == defaultType.stone){
                cc.Component.EventHandler.emitEvents(this.toolEvent, []);
            }else if(this.curTriggerType == triggerType.boxTrigger && other.node.getComponent("defaultTypeMediator").curType == defaultType.box ){
                cc.Component.EventHandler.emitEvents(this.toolEvent, []);
            }
        }else{
            if(this.curTriggerType == triggerType.normal){
                if(this.dirJudge){
                    if(this.node.x < other.node.x){
                        cc.Component.EventHandler.emitEvents(this.toolEvent, [2]);
                    }else{
                        cc.Component.EventHandler.emitEvents(this.toolEvent, [1]);
                    }
                }else{
                    if(this.crashRole && other.node.group == "key"){
                        // self.node.destroy();
                    }else{
                        self.node.destroy();
                        cc.Component.EventHandler.emitEvents(this.toolEvent, []);
                    }
                }
            }else if(this.curTriggerType == triggerType.delay){
                this.standing = true;
                this.scheduleOnce(()=>{
                    if(this.standing){
                        cc.Component.EventHandler.emitEvents(this.toolEvent, []);
                    }
                },3)
            }else if(this.curTriggerType == triggerType.show){
                if(other.node.group == "ghost" && this.checkGhost) return;
                cc.Component.EventHandler.emitEvents(this.toolEvent, [self.node,other.node,1]);
            }
        }
    }

    onEndContact(contact, self, other) {
        if(this.curTriggerType == triggerType.delay){
            this.standing = false;
        }else if(this.curTriggerType == triggerType.show){
            if(other.node.group == "ghost" && this.checkGhost) return;
            cc.Component.EventHandler.emitEvents(this.toolEvent, [self.node,other.node,2]);
        }
    }
}
